package com.food.admin.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.food.admin.dto.StoreRegisterDetailDto;
import com.food.admin.dto.StoreRegisterListDto;
import com.food.admin.exception.MyParameterException;
import com.food.admin.form.StoreRegisterListForm;
import com.food.admin.form.StoreRegisterRejectForm;
import com.food.admin.form.StoreRegisterIdForm;
import com.food.admin.model.Result;
import com.food.admin.service.StoreRegisterService;
import com.food.admin.util.DateUtils;
import com.food.admin.util.MyPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 商家注册表 前端控制器
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月13日
 */
@RestController
@RequestMapping("/storeRegister")
@Api(tags = "商铺注册接口")
public class StoreRegisterController {
    @Autowired
    private StoreRegisterService srs;

    @GetMapping("/list")
    @ApiOperation("分页条件查询商铺注册列表")
    public Result listByPageAndCondition(@Valid StoreRegisterListForm form, BindingResult br) throws Exception {

        if (br.hasErrors()) {
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                //获取打印提示信息
                System.out.println(allError.getDefaultMessage());
            }
            throw new MyParameterException(500, "参数校验失败");
        }

        MyPage<StoreRegisterListDto> lists= srs.listByPageAndCondition(form);
        return Result.success("200","分页条件查询商铺注册表成功",lists);
    }

    @GetMapping("/detail")
    @ApiOperation("查看商铺注册详情")
    public Result viewDetail(@Valid StoreRegisterIdForm form, BindingResult br){

        if (br.hasErrors()){
            throw new MyParameterException(500,"参数校验失败");
        }
        StoreRegisterDetailDto detailDto= srs.getDetail(form);
        return Result.success("200","查询商铺注册详情成功",detailDto);
    }

    @PostMapping("/reject")
    @ApiOperation("驳回申请")
    public Result rejectRegister(@Valid @RequestBody StoreRegisterRejectForm form, BindingResult br) throws Exception{

        srs.rejectRegister(form);
        return Result.success("200","驳回商家申请成功",null);
    }

    @PostMapping("/pass")
    @ApiOperation("通过申请")
    public Result passRegister(@Valid @RequestBody StoreRegisterIdForm form, BindingResult br) throws Exception{
        if (br.hasErrors()){
            throw new MyParameterException(500,"参数校验失败");
        }
        srs.passRegister(form);
        return Result.success("200","审核通过商家申请",null);
    }


}

